package org.github.manx98.jlibhac.tools.fssystem.romfs;

import org.github.manx98.jlibhac.fs.fsa.IFile;

import java.io.IOException;
import java.util.function.Supplier;

/**
 * @author Manx98
 * @version 1.0
 * @since 2022/10/4 2:11
 */
public class RomfsHeader {
    public long HeaderSize;
    public long DirHashTableOffset;
    public long DirHashTableSize;
    public long DirMetaTableOffset;
    public long DirMetaTableSize;
    public long FileHashTableOffset;
    public long FileHashTableSize;
    public long FileMetaTableOffset;
    public long FileMetaTableSize;
    public long DataOffset;

    public RomfsHeader(IFile reader) throws IOException {

        HeaderSize = reader.readInt();

        // Old pre-release romfs is exactly the same except the fields in the header are 32-bit instead of 64-bit
        if (HeaderSize == 0x28) {
            DirHashTableOffset = reader.readInt();
            DirHashTableSize = reader.readInt();
            DirMetaTableOffset = reader.readInt();
            DirMetaTableSize = reader.readInt();
            FileHashTableOffset = reader.readInt();
            FileHashTableSize = reader.readInt();
            FileMetaTableOffset = reader.readInt();
            FileMetaTableSize = reader.readInt();
            DataOffset = reader.readInt();
        } else {
            reader.readBytes(4);
            DirHashTableOffset =reader.readLong();
            DirHashTableSize =reader.readLong();
            DirMetaTableOffset =reader.readLong();
            DirMetaTableSize =reader.readLong();
            FileHashTableOffset =reader.readLong();
            FileHashTableSize =reader.readLong();
            FileMetaTableOffset =reader.readLong();
            FileMetaTableSize =reader.readLong();
            DataOffset =reader.readLong();
        }
    }
}
